﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Windows.Threading
Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient

Namespace ViewModel

    Public Class ListBlobsPageViewModel
        Inherits PivotItemViewModel
        Private Const IconsRootUri As String = "/Toolkit.Content/"

        Private ReadOnly blobClient As ICloudBlobClient
        Private ReadOnly dispatcher As Dispatcher

        Private isListingValue As Boolean = False
        Private hasResultsValue As Boolean = True
        Private useFlatBlobListingValue As Boolean = False
        Private prefixValue As String = String.Empty
        Private messageValue As String = String.Empty

        Public Sub New()
            Me.New(App.CloudClientFactory.ResolveCloudBlobClient("UploadAndList"), Deployment.Current.Dispatcher)
        End Sub

        Public Sub New(ByVal blobClient As ICloudBlobClient, ByVal dispatcher As Dispatcher)
            Me.blobClient = blobClient
            Me.dispatcher = dispatcher
            Me.CloudBlobs = New ObservableCollection(Of ICloudBlob)()
        End Sub

        Public Event TakePhoto As EventHandler(Of EventArgs)

        Public Property CloudBlobs() As ObservableCollection(Of ICloudBlob)

        Public Property IsListing() As Boolean
            Get
                Return Me.isListingValue
            End Get

            Set(ByVal value As Boolean)
                If Me.isListingValue <> value Then
                    Me.isListingValue = value
                    Me.NotifyPropertyChanged("IsListing")
                End If
            End Set
        End Property

        Public Property Message() As String
            Get
                Return Me.messageValue
            End Get

            Set(ByVal value As String)
                If Me.messageValue <> value Then
                    Me.messageValue = value
                    Me.NotifyPropertyChanged("Message")
                End If
            End Set
        End Property

        Public Property HasResults() As Boolean
            Get
                Return Me.hasResultsValue
            End Get

            Set(ByVal value As Boolean)
                If Me.hasResultsValue <> value Then
                    Me.hasResultsValue = value
                    Me.NotifyPropertyChanged("HasResults")
                End If
            End Set
        End Property

        Public Property Prefix() As String
            Get
                Return Me.prefixValue
            End Get

            Set(ByVal value As String)
                If Me.prefixValue <> value Then
                    Me.prefixValue = value
                    Me.NotifyPropertyChanged("Prefix")
                End If
            End Set
        End Property

        Public Property UseFlatBlobListing() As Boolean
            Get
                Return Me.useFlatBlobListingValue
            End Get

            Set(ByVal value As Boolean)
                If Me.useFlatBlobListingValue <> value Then
                    Me.useFlatBlobListingValue = value
                    Me.NotifyPropertyChanged("UseFlatBlobListing")
                End If
            End Set
        End Property

        Public Sub ListBlobs()
            Me.IsListing = True
            Me.Message = "Listing blobs..."
            Try

                Me.blobClient.ListBlobsWithPrefix(
                    Me.Prefix,
                    Me.UseFlatBlobListing,
                    Sub(r)
                        If Me.dispatcher IsNot Nothing Then
                            Me.dispatcher.BeginInvoke(Sub() Me.UpdateListBlobs(r))
                        Else
                            Me.UpdateListBlobs(r)
                        End If
                    End Sub)
            Catch ex As Exception
                Dim errorMessage = StorageClientExceptionParser.ParseDataServiceException(ex).Message

                Me.IsListing = False
                Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", errorMessage)
            End Try
        End Sub

        Protected Overrides Sub PopulateApplicationBarButtons(ByVal applicationBar As IApplicationBar)
            applicationBar.Buttons.Clear()

            Dim takePictureButton = New ApplicationBarIconButton(New Uri(String.Format("{0}{1}", IconsRootUri, "appbar.feature.camera.rest.png"), UriKind.Relative)) With {.Text = "take picture"}
            AddHandler takePictureButton.Click, Sub(s, e) Me.RaiseTakePhoto()

            applicationBar.Buttons.Add(takePictureButton)
        End Sub

        Private Sub UpdateListBlobs(ByVal result As CloudOperationResponse(Of IEnumerable(Of ICloudBlob)))
            If result.Exception Is Nothing Then
                Me.CloudBlobs.Clear()
                For Each blob In result.Response
                    Me.CloudBlobs.Add(blob)
                Next blob

                Me.HasResults = Me.CloudBlobs.Count > 0
                Me.Message = "Listing finished successfully."
            Else
                Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", result.Exception.Message)
            End If

            Me.IsListing = False
        End Sub

        Private Sub RaiseTakePhoto()
            RaiseEvent TakePhoto(Me, New EventArgs())
        End Sub

    End Class
End Namespace